<!--决算单详情-->
<template>
  <div class="acct-balance-detail ne-relative">
    <div style="height: 100%;overflow-y: auto;">
      <el-form
          ref="TcForm2"
          label-position="left"
          label-width="1rem"
          :model="Form"
      >
        <el-row>
          <tc-form2 :form="Form"></tc-form2>
          <h2 class="ne-title-v2">一次性费用决算通知单 {{this.$util.dateFormat(new Date(),"yyyy-MM-dd")}}</h2>
          <DicTable
              height="100%"
              stripe
              max-height="600"
              ref="GQTable"
              border
              header-row-class-name="header-row-class"
              :columnData="GQtbs.tableColumns"
              :tableData="GQtbs.tableData"
              :loading="GQtbs.tableLoading"
              :pager="false"
          >
            <template #num="{ row,index }">
              <div class="flex">
                <p>{{index+1}}</p>
              </div>
            </template>
          </DicTable>
          <tc-form2 class="mar-20" :form="FormB"></tc-form2>
        </el-row>
        <el-row class="fixed">
          <el-col :span="24" class="ne-line-r flex flex-center">
              <el-button type="primary" size="medium" @click="save">保存</el-button>
              <el-button type="primary" size="medium">输出Excel</el-button>
              <el-button size="medium" @click="closeFun">关闭</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <el-dialog append-to-body :close-on-click-modal='false' :title="dialogTitle" v-if="AuditdialogVisible" :visible.sync="AuditdialogVisible">
      <el-form v-if="audit" :model="audName">
        <el-form-item :label="audName.cName" label-width="1rem">
          <el-select v-model="audName.cValue" placeholder="请选择">
            <el-option
                v-for="(item,index) in audName.opt"
                :key="index"
                :label="item.label"
                :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form v-else :model="gryBillAudName">
        <el-form-item :label="gryBillAudName.cName" label-width="1rem">
          <el-select v-model="gryBillAudName.cValue" placeholder="请选择">
            <el-option
                v-for="(item,index) in gryBillAudName.opt"
                :key="index"
                :label="item.label"
                :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="send">发送</el-button>
        <el-button @click="AuditdialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import TcForm2 from "@/components/public/TcForm2";
import DicTable from "@/components/public/DicTable";
import CustOrderService from "@/api/order/CustOrderService";
import {mapState} from "vuex";

export default {
  name: "AcctBalanceInfo",
  data() {
    return {
      audit: true,
      dialogTitle: '',
      gryBillAudName: {
        type: 'select',
        cName: '审核人',
        cValue: '',
        required: true,
        opt: [],
        show: true,
        span: 24,
      },
      Form: {
        "custSoNumber": {
          type: 'Ipt',
          name: '流水号',
          readonly: true,
          value: "",
          id: "custSoNumber",
          show: true,
          span: 6
        },
        "custName": {
          type: 'Ipt',
          name: '客户名称',
          readonly: true,
          value: "",
          id: "custName",
          show: true,
          span: 6
        },
        "contactCd": {
          type: 'Ipt',
          name: '合同编码',
          readonly: true,
          value: "",
          id: "contactCd",
          show: true,
          span: 6,
          marginRight: '25%'
        },
        "accountUnitName": {
          type: 'Ipt',
          name: '收款单位',
          readonly: true,
          value: "",
          id: "accountUnitName",
          show: true,
          span: 6
        },
        "accountName": {
          type: 'Ipt',
          name: '账户',
          readonly: true,
          value: "",
          id: "accountName",
          show: true,
          span: 6
        },
        "bank": {
          type: 'Ipt',
          name: '开户银行',
          readonly: true,
          value: "",
          id: "bank",
          show: true,
          span: 6,
          marginRight: '25%'
        },
        "contactName": {
          type: 'Ipt',
          name: '联系人',
          readonly: true,
          value: "",
          id: "contactName",
          show: true,
          span: 6
        },
        "mobilePhone": {
          type: 'Ipt',
          name: '联系电话',
          readonly: true,
          value: "",
          id: "mobilePhone",
          show: true,
          span: 6,
          marginRight: '50%'
        },
        "allRmb": {
          type: 'Ipt',
          name: '合计',
          readonly: true,
          value: "￥0",
          id: "moneyRmb",
          show: true,
          span: 6
        },
        "rmbBig": {
          type: 'Ipt',
          name: '大写(元)',
          readonly: true,
          value: "零元",
          id: "rmbBig",
          show: true,
          span: 6
        },
        "rmbSmall": {
          type: 'Ipt',
          name: '小计',
          readonly: true,
          value: "￥0",
          id: "moneyRmb",
          show: true,
          span: 6,
          marginRight: '25%'
        }
      },
      FormB: {
        "rmbxj": {
          type: 'Ipt',
          name: '人民币小计',
          readonly: true,
          value: "￥0",
          id: "moneyRmb",
          show: true,
          span: 6,
          marginRight: '75%'
        },
        "moneySum": {
          type: 'Ipt',
          name: '合计',
          readonly: true,
          value: "￥0",
          id: "moneyRmb",
          show: true,
          span: 6,
          marginRight: '75%'
        },
        "billComment": {
          type: 'textarea',
          name: '备注',
          value: "",
          id: "billComment",
          show: true,
          span: 12,
          marginRight: '50%'
        },
      },
      GQtbs: {
        tableColumns: [
          {slotName:"num", title: "序号", width: "5%"},
          {field: "regionRange", title: "单位"},
          {field: "offerName", title: "业务种类"},
          {field: "speed", title: "速率"},
          {field: "custOrderType", title: "类型"},
          {field: "moneyRmbMat", title: "工料费"},
          {field: "moneyRmbTest", title: "调测费"},
          {field: "moneyRmbPro", title: "手续费"},
          { field: "subtotal", title: "小计"},
          {field: "moneyRmbEng", title: "工程费"},
          {field: "moneyRmbDev", title: "设备费"},
          { field: "receivables", title: "应收款"}
        ],
        tableData: [],
        tableLoading: false,
      },
      audName: {
        type: 'select',
        cName: '审核人',
        cValue: '',
        required: true,
        opt: [],
        show: true,
        span: 24,
      },
      taskId: null,
      AuditdialogVisible: false
    }
  },
  props: {
    rowInfo: {
      type: Object
    },
    allInfo: {
      type: Object
    }
  },
  mounted() {

  },
  computed: {
    ...mapState('user', ['user'])
  },
  created() {
    this.getDetail();
  },
  components: {TcForm2, DicTable},
  methods: {
    async getDetail(){
      // acctItemListId 非必填字段，列表中点击进入需要填
      let{custOrderId,custSoNumber,custId,batchNbr} = this.allInfo,
          {acctItemListId} = this.rowInfo,
          params = {batchNbr,acctItemListId,custOrderId,custSoNumber,custId,...{userId:this.user.staffId,staffId:this.user.staffId}};
      const result = await CustOrderService.qryFinalAccountDetails(params),
          res = result.meta;
      if(res.resultCode == '0'){
        if(res.hasOwnProperty('acctItems')) {
          this.GQtbs.tableData = this.tableDataInit(res.acctItems);
        };
          for(var key in res.acctItemLists[0]){
            let value = res.acctItemLists[0][key];
            for(var key2 in this.Form){
              if(this.Form[key2].id == key){
                if(this.Form[key2].id == 'moneyRmb'){
                  this.Form[key2].value = '￥'+value;
                }else{
                  this.Form[key2].value = value;
                }
              }
            }
            for(var key2 in this.FormB){
              if(this.FormB[key2].id == key){
                if(this.FormB[key2].id == 'moneyRmb'){
                  this.FormB[key2].value = '￥'+value;
                }else{
                  this.FormB[key2].value = value;
                }
              }
            }
        };
        if(res.acctItemLists[0].hasOwnProperty('moneyRmb')){
          const result2= await CustOrderService.transferAmount({amount: res.acctItemLists[0].moneyRmb});
          this.Form.rmbBig.value = result2.meta.amount;
        };
      }else{
        this.$message({
          showClose: true,
          message: result.resultMsg,
          type: 'error'
        });
      }
    },
    // 保存未测试，待自己测试
    async save() {
      let {custSoNumber, custId} = this.allInfo;
      let params = {
        custId: custId,
        custSoNumber: custSoNumber,
        userId: this.user.staffId,
        orgId: this.user.orgId,
        userName: this.user.staffName,
        orderAuditId: this.rowInfo.orderAuditId,
        acctItemLists: [{
          custId: custId,
          lastModifyBy: this.user.staffId,
          lastModifyDate: this.$util.dateFormat(new Date(), "yyyy-MM-dd hh:mm:ss"),
          actType: "MOD",
          billComment: this.FormB.billComment.value,
          acctItemListId: this.rowInfo.acctItemListId
        }],
      }
      const result = await CustOrderService.commonOrderUpdate(params);
      if (result.meta.resultCode == 0) {
        this.$message({
          type: 'success',
          message: '保存成功!',
        });
        this.getDetail();
      } else {
        this.$message({
          showClose: true,
          message: result.meta.resultMsg,
          type: 'error',
        });
      }
    },
    tableDataInit(arr){
      
      
      let le = {moneyRmbMat:"0",moneyRmbTest:"0",moneyRmbPro:"0",moneyRmbEng:"0",moneyRmbDev:"0"}
      let newarr = [];
      arr.map((item,index)=>{
        let les = {...le};
        Object.assign(les,item);
        item = {...les};
        item.moneyRmbMat = parseFloat(Number(item.moneyRmbMat).toFixed(2));
        item.moneyRmbTest = parseFloat(Number(item.moneyRmbTest).toFixed(2));
        item.moneyRmbPro = parseFloat(Number(item.moneyRmbPro).toFixed(2));
        item.moneyRmbEng = parseFloat(Number(item.moneyRmbEng).toFixed(2));
        item.moneyRmbDev = parseFloat(Number(item.moneyRmbDev).toFixed(2));
        item.subtotal = '￥'+(Number(item.moneyRmbMat)+Number(item.moneyRmbTest)+Number(item.moneyRmbPro));
        item.receivables = '￥'+(Number(item.moneyRmbMat)+Number(item.moneyRmbTest)+Number(item.moneyRmbPro)+Number(item.moneyRmbEng)+Number(item.moneyRmbDev));
        for(var key in item){
          if(le[key]){
            item[key] = '￥'+item[key];
          }
        }
        newarr.push(item);
      })
      return newarr;
    },
    closeFun() {
      this.$emit('update:show', false)
    },
  }
}
</script>

<style lang="scss" scoped>
.acct-balance-detail {
  height: 84vh;
  padding-bottom: .3rem;

  .ne-line-r {
    & > div {
      margin-right: .1rem;
    }
  }

  .fixed {
    position: absolute;
    bottom: -.1rem;
    left: 0;
    right: 0;
    margin: 0 auto;
  }
}
</style>
